import  {getMenu} from  '@/api/index'
import router from '@/router/index'
 export function routesData(result) {
  let  menus = []
   menus = result.map((item, index) => {
      //父路由
      let menu ={
        path:item.menuUrl,
        name:item.name,
        meta:{title:item.menuName,icon:item.iconName,top:true}
      }
      //子路由
      if(item.subMenus&&item.subMenus.length){
        menu.children = item.subMenus.map(child=>({
            path: child.menuUrl,
            name: child.name,
            meta: { title: child.menuName, icon: child.iconName },
             component: () =>import(`../views/${item.name}/${child.component}.vue`)  
        }))
      } else {
      menu.component = () => import(`../views/${item.name}/${item.component}.vue`)
    }
    return menu
  })
    //添加个人信息
    menus.push({
            path: "/modifyPwd",
            name: "ModifyPwd",
            meta: {
                title: "修改密码",
                icon: "PieChart"
            },
            component: () => import('@/views/profile/ModifyPwd.vue')
        }),
        menus.push( {
            path: "/info",
            name: "Info",
            meta: {
                title: "个人信息",
                icon: "Histogram"
            },
            component: () => import('@/views/profile/Info.vue')
        })
    return menus
 }  


export const  flushMenu=async ()=>{
    let  token  = localStorage.getItem("token")
    if(token){
        console.log(111)
        let menus = []
        await   getMenu().then(res=>{
            //路由拼接
           menus =   routesData(res.data.menus)
            console.log('完成拼接之后的menus:', menus)
        }).catch(error=>{
            console.log("加载数据失败。。。",error);
        })
        menus.forEach(item=>{
            router.addRoute('home',item)
        })
    }
 }